      subroutine systempsimage(c,nx,nz,deltax,deltaz,f1,f2,filename,units,label1,label2,&
      & width,d1num,d2num,x1beg,x1end,x2beg,x2end,iprint)

      implicit none
      integer :: i,j
      integer :: iprint
      double precision:: c(nx,nz)
      integer :: nx, nz
      character*5 :: filename
      character*20 :: units,label1,label2
      double precision :: width,height,lx
      double precision :: deltax,deltaz,d1num,d2num
      double precision :: lbeg,lend,ldnum
      character*10 :: chlbeg,chlend,chldnun
      character*1, parameter:: apos=achar(39)! apostrophe
      character*5 :: chn1,chn2
      character*10 :: chdeltax,chdeltaz,chd1num,chd2num,chwidth,chheight,chlx,chldnum
      character*10 :: chf1,chf2,chx1beg,chx1end,chx2beg,chx2end
      double precision :: f1,f2,x1beg,x1end,x2beg,x2end
      real, dimension(:,:), allocatable:: creal4 !velocidade de propagacao




      if(iprint.eq.1)then
	  write(*,*)
	  write(*,*)
	  write(*,*)'*******************************  PSIMAGE  *************************************'
      endif



      allocate(creal4(Nx,Nz))
      do j=1,Nz
	do i=1,Nx
	  creal4(i,j) = SNGL(c(i,j))
	enddo
      enddo

      open(1,file=filename//'.bin',status="unknown",form="unformatted",access="direct",recl=4*Nx*Nz)
      write(1,rec=1)(((creal4(i,j)),j=1,Nz),i=1,Nx)
      close(1)

      deallocate(creal4)


      lbeg=c(1,1)
      lend=c(1,1)
      do j=1,nz
	  do i=1,Nx
	      if(c(i,j).lt.lbeg)then
		  lbeg=c(i,j)
	      endif
	      if(c(i,j).gt.lend)then
		  lend=c(i,j)
	      endif
	  enddo
      enddo
      ldnum=1000.d0
      lbeg=lbeg-mod(lbeg,1000.d0)
      lend=lend-mod(lend,1000.d0)+1000.d0
      
	      

      
	  write(chlbeg,'(E10.4)')lbeg
!       if(lbeg.lt.10)then
! 	  write(chlbeg,'(f2.0)')lbeg
!       elseif(lbeg.lt.100)then
! 	  write(chlbeg,'(f3.0)')lbeg
!       elseif(lbeg.lt.1000)then
! 	  write(chlbeg,'(f4.0)')lbeg
!       elseif(lbeg.lt.10000)then
! 	  write(chlbeg,'(f5.0)')lbeg
!       endif

	  write(chlend,'(E10.4)')lend
!       if(lend.lt.10)then
! 	  write(chlend,'(f2.0)')lend
!       elseif(lend.lt.100)then
! 	  write(chlend,'(f3.0)')lend
!       elseif(lend.lt.1000)then
! 	  write(chlend,'(f4.0)')lend
!       elseif(lend.lt.10000)then
! 	  write(chlend,'(f5.0)')lend
!       endif


	  write(chldnum,'(E10.4)')ldnum
!       if(ldnum.lt.10.)then
! 	  write(chldnum,'(f4.2)')ldnum
!       elseif(ldnum.lt.100.)then
! 	  write(chldnum,'(f5.2)')ldnum
!       elseif(ldnum.lt.1000.)then
! 	  write(chldnum,'(f6.2)')ldnum
!       elseif(ldnum.lt.10000.)then
! 	  write(chldnum,'(f7.2)')ldnum
!       endif



      if(nz.lt.10)then
	  write(chn1,'(i1)')nz
	  chn1 = '0000'//chn1
      elseif(nz.lt.100)then
	  write(chn1,'(i2)')nz
	  chn1 = '000'//chn1
      elseif(nz.lt.1000)then
	  write(chn1,'(i3)')nz
	  chn1 = '00'//chn1
      elseif(nz.lt.10000)then
	  write(chn1,'(i4)')nz
	  chn1 = '0'//chn1
      elseif(nz.lt.100000)then
	  write(chn1,'(i5)')nz
      endif


      if(nx.lt.10)then
	  write(chn2,'(i1)')nx
	  chn2 = '0000'//chn2 
      elseif(nx.lt.100)then
	  write(chn2,'(i2)')nx
	  chn2 = '000'//chn2 
      elseif(nx.lt.1000)then
	  write(chn2,'(i3)')nx
	  chn2 = '00'//chn2
      elseif(nx.lt.10000)then
	  write(chn2,'(i4)')nx
	  chn2 = '0'//chn2
      elseif(nx.lt.100000)then
	  write(chn2,'(i5)')nx
      endif



      write(chd1num,'(E10.4)')d1num
!       if(d1num.lt.10.)then
! 	  write(chd1num,'(f4.2)')d1num
!       elseif(d1num.lt.100.)then
! 	  write(chd1num,'(f5.2)')d1num
!       elseif(d1num.lt.1000.)then
! 	  write(chd1num,'(f6.2)')d1num
!       elseif(d1num.lt.10000.)then
! 	  write(chd1num,'(f7.2)')d1num
!       endif

      write(chd2num,'(E10.4)')d2num
!       if(d2num.lt.10.)then
! 	  write(chd2num,'(f4.2)')d2num
!       elseif(d2num.lt.100.)then
! 	  write(chd2num,'(f5.2)')d2num
!       elseif(d2num.lt.1000.)then
! 	  write(chd2num,'(f6.2)')d2num
!       elseif(d2num.lt.10000.)then
! 	  write(chd2num,'(f7.2)')d2num
!       endif



	  write(chdeltax,'(E10.4)')deltax
!       if(deltax.lt.10.)then
! 	  write(chdeltax,'(f4.2)')deltax
!       elseif(deltax.lt.100.)then
! 	  write(chdeltax,'(f5.2)')deltax
!       elseif(deltax.lt.1000.)then
! 	  write(chdeltax,'(f6.2)')deltax
!       elseif(deltax.lt.10000.)then
! 	  write(chdeltax,'(f7.2)')deltax
!       endif

	  write(chdeltaz,'(E10.4)')deltaz
!       if(deltaz.lt.10.)then
! 	  write(chdeltaz,'(f4.2)')deltaz
!       elseif(deltaz.lt.100.)then
! 	  write(chdeltaz,'(f5.2)')deltaz
!       elseif(deltaz.lt.1000.)then
! 	  write(chdeltaz,'(f6.2)')deltaz
!       elseif(deltaz.lt.10000.)then
! 	  write(chdeltaz,'(f7.2)')deltaz
!       endif


	  write(chwidth,'(E10.4)')width
!       if(width.lt.10.)then
! 	  write(chwidth,'(f4.2)')width
!       elseif(width.lt.100.)then
! 	  write(chwidth,'(f5.2)')width
!       elseif(width.lt.1000.)then
! 	  write(chwidth,'(f6.2)')width
!       elseif(width.lt.10000.)then
! 	  write(chwidth,'(f7.2)')width
!       endif


      height = (0.75d0*width)/(x2end-x2beg)*(x1end-x1beg)

	write(chheight,'(E10.4)')height
!       if(height.lt.10.)then
! 	  write(chheight,'(f4.2)')height
!       elseif(height.lt.100.)then
! 	  write(chheight,'(f5.2)')height
!       elseif(height.lt.1000.)then
! 	  write(chheight,'(f6.2)')height
!       elseif(height.lt.10000.)then
! 	  write(chheight,'(f7.2)')height
!       endif

      write(chf1,'(E10.4)')f1
!       if(f1.ge.0)write(chf1,'(E10.4)')f1
!       if(f1.lt.0)write(chf1,'(E9.4)')f1
!       if(abs(f1).lt.10.)then
! 	  if(f1.ge.0)write(chf1,'(f4.2)')f1
! 	  if(f1.lt.0)write(chf1,'(f5.2)')f1
!       elseif(abs(f1).lt.100.)then
! 	  if(f1.ge.0)write(chf1,'(f5.2)')f1
! 	  if(f1.lt.0)write(chf1,'(f6.2)')f1
!       elseif(abs(f1).lt.1000.)then
! 	  if(f1.ge.0)write(chf1,'(f6.2)')f1
! 	  if(f1.lt.0)write(chf1,'(f7.2)')f1
!       elseif(abs(f1).lt.10000.)then
! 	  if(f1.ge.0)write(chf1,'(f7.2)')f1
! 	  if(f1.lt.0)write(chf1,'(f8.2)')f1
!       endif

      write(chf2,'(E10.4)')f2
!       if(abs(f2).lt.10.)then
! 	  if(f2.ge.0)write(chf2,'(f4.2)')f2
! 	  if(f2.lt.0)write(chf2,'(f5.2)')f2
!       elseif(abs(f2).lt.100.)then
! 	  if(f2.ge.0)write(chf2,'(f5.2)')f2
! 	  if(f2.lt.0)write(chf2,'(f6.2)')f2
!       elseif(abs(f2).lt.1000.)then
! 	  if(f2.ge.0)write(chf2,'(f6.2)')f2
! 	  if(f2.lt.0)write(chf2,'(f7.2)')f2
!       elseif(abs(f2).lt.10000.)then
! 	  if(f2.ge.0)write(chf2,'(f7.2)')f2
! 	  if(f2.lt.0)write(chf2,'(f8.2)')f2
!       endif

      write(chx1beg,'(E10.4)')x1beg
      write(chx1end,'(E10.4)')x1end
      write(chx2beg,'(E10.4)')x2beg
      write(chx2end,'(E10.4)')x2end

      if(iprint.eq.1)then
      write(*,*)'psimage <'//filename//'.bin'//' n1='//chn1//' d1='//chdeltaz//' n2='//chn2//' d2='//chdeltax//&
		& ' f1='//chf1//' f2='//chf2//' f1num=0.00'//' f2num=0.00'//&
		& ' x1beg='//chx1beg//' x1end='//chx1end//' x2beg='//chx2beg//' x2end='//chx2end//&
! 		& ' threecolor=1'//' brgb=1,0,0'//' grgb=0.30,0.8,0.3'//' wrgb=0,0,1'//&
		& ' legend=1'//' units='//apos//units//apos//' label1='//apos//label1//apos//' label2='//apos//label2//apos//&
		& ' width='//chwidth//' height='//chheight//' lx=0.0'//' d1num='//chd1num//' d2num='//chd2num//&
		& ' lend='//chlend//' lbeg='//chlbeg//' ldnum='//chldnum//&
		& ' bclip='//chlend//' wclip='//chlbeg//&
		& ' legendfont=times_roman8 labelsize=12 titlesize=18 verbose=0 >'//filename//'.ps &'
      endif

      call system('psimage <'//filename//'.bin'//' n1='//chn1//' d1='//chdeltaz//' n2='//chn2//' d2='//chdeltax//&
		& ' f1='//chf1//' f2='//chf2//' f1num=0.00'//' f2num=0.00'//&
		& ' x1beg='//chx1beg//' x1end='//chx1end//' x2beg='//chx2beg//' x2end='//chx2end//&
! 		& ' threecolor=1'//' brgb=1,0,0'//' grgb=0.30,0.8,0.3'//' wrgb=0,0,1'//&
		& ' legend=1'//' units='//apos//units//apos//' label1='//apos//label1//apos//' label2='//apos//label2//apos//&
		& ' width='//chwidth//' height='//chheight//' lx=0.0'//' d1num='//chd1num//' d2num='//chd2num//&
		& ' lend='//chlend//' lbeg='//chlbeg//' ldnum='//chldnum//&
		& ' bclip='//chlend//' wclip='//chlbeg//&
		& ' legendfont=times_roman8 labelsize=12 titlesize=18 verbose=0 >'//filename//'.ps &')




      return

!  150  format(//a450//)
      end